package com.android.tianyuanyouxuan.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;

import com.android.tianyuanyouxuan.App;
import com.android.tianyuanyouxuan.base.BaseActivity;
import com.android.tianyuanyouxuan.bean.Circle;
import com.android.tianyuanyouxuan.databinding.ActivityAddCircleBinding;
import com.android.tianyuanyouxuan.util.GlideEngine;
import com.android.tianyuanyouxuan.util.UserData;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.Date;

public class AddCircleActivity extends BaseActivity {

    private ActivityAddCircleBinding viewBinding;
    private String selectPic = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityAddCircleBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        initTitleBar();
        mTitleBar.getRightView().setText("发布");
        viewBinding.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PictureSelector.create(AddCircleActivity.this)
                        .openGallery(SelectMimeType.ofImage())
                        .setMaxSelectNum(1)
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (!CollectionUtils.isEmpty(result)) {
                                    String path = result.get(0).getRealPath();
                                    Glide.with(AddCircleActivity.this)
                                            .load(path)
                                            .into(viewBinding.ivImage);
                                    selectPic = path;
                                }
                            }

                            @Override
                            public void onCancel() {

                            }
                        });

            }
        });
    }

    @Override
    public void onRightClick(TitleBar titleBar) {
        super.onRightClick(titleBar);
        if (TextUtils.isEmpty(viewBinding.etTitle.getText())) {
            ToastUtils.showShort("请输入标题");
            return;
        }
        if (TextUtils.isEmpty(viewBinding.etText.getText())) {
            ToastUtils.showShort("请输入内容");
            return;
        }
        Circle circle = new Circle();
        circle.setTitle(viewBinding.etTitle.getText().toString());
        circle.setText(viewBinding.etText.getText().toString());
        circle.setImage(selectPic);
        circle.setUser(UserData.user.name);
        circle.setDate(new Date());
        App.mApp.getDaoSession().getCircleDao().insert(circle);
        ToastUtils.showShort("发布成功");
        finish();
    }
}
